

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="python" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="python" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>QUANTAXIS.QAMarket.tdxRealBroker &mdash; QUANTAXIS 1.0.39 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> QUANTAXIS
          

          
          </a>

          
            
            
              <div class="version">
                1.0.39
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">QUANTAXIS</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>QUANTAXIS.QAMarket.tdxRealBroker</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for QUANTAXIS.QAMarket.tdxRealBroker</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding:utf-8</span>


<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">urllib</span>
<span class="kn">import</span> <span class="nn">base64</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">cryptography.hazmat.backends</span> <span class="k">import</span> <span class="n">default_backend</span>
<span class="kn">from</span> <span class="nn">cryptography.hazmat.primitives.ciphers</span> <span class="k">import</span> <span class="n">Cipher</span><span class="p">,</span> <span class="n">algorithms</span><span class="p">,</span> <span class="n">modes</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAMarket.QABroker</span> <span class="k">import</span> <span class="n">QA_Broker</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">pytdx.log</span> <span class="k">import</span> <span class="n">log</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">log</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> 
        <span class="k">return</span> <span class="kc">None</span>


<div class="viewcode-block" id="TdxTradeApiParams"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TdxTradeApiParams">[docs]</a><span class="k">class</span> <span class="nc">TdxTradeApiParams</span><span class="p">:</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    0 资金</span>
<span class="sd">    1 股份</span>
<span class="sd">    2 当日委托</span>
<span class="sd">    3 当日成交</span>
<span class="sd">    4 可撤单</span>
<span class="sd">    5 股东代码</span>
<span class="sd">    6 融资余额</span>
<span class="sd">    7 融券余额</span>
<span class="sd">    8 可融证券</span>
<span class="sd">    9</span>
<span class="sd">    10</span>
<span class="sd">    11</span>
<span class="sd">    12 可申购新股查询</span>
<span class="sd">    13 新股申购额度查询</span>
<span class="sd">    14 配号查询</span>
<span class="sd">    15 中签查询</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">QUERY_CATEGORY_CASH</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">QUERY_CATEGORY_STOCKS</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">QUERY_CATEGORY_ORDER_OF_TODAY</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="n">QUERY_CATEGORY_DEAL_OF_TODAY</span> <span class="o">=</span> <span class="mi">3</span>
    <span class="n">QUERY_CATEGORY_CANCELABLE_ORDER</span> <span class="o">=</span> <span class="mi">4</span>
    <span class="n">QUERY_CATEGORY_SHAREHOLDERS_CODE</span> <span class="o">=</span> <span class="mi">5</span>
    <span class="n">QUERY_CATEGORY_BALANCE_OF_MARGIN_LOAN</span> <span class="o">=</span> <span class="mi">6</span>
    <span class="n">QUERY_CATEGORY_BALANCE_OF_STOCK_LOAN</span> <span class="o">=</span> <span class="mi">7</span>
    <span class="n">QUERY_CATEGORY_OPERABLE_MARGIN_SOTCK</span> <span class="o">=</span> <span class="mi">8</span>

    <span class="n">QUERY_CATEGORY_NEW_STOCKS</span> <span class="o">=</span> <span class="mi">12</span>
    <span class="n">QUERY_CATEGORY_NEW_STOCKS_QUOTA</span> <span class="o">=</span> <span class="mi">13</span>
    <span class="n">QUERY_CATEGORY_NEW_STOCK_NUMBER</span> <span class="o">=</span> <span class="mi">14</span>
    <span class="n">QUERY_CATEGORY_NEW_STOCK_HIT</span> <span class="o">=</span> <span class="mi">15</span></div>


<div class="viewcode-block" id="TDXBroker"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker">[docs]</a><span class="k">class</span> <span class="nc">TDXBroker</span><span class="p">(</span><span class="n">QA_Broker</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s2">&quot;http://127.0.0.1:10092/api&quot;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">,</span> <span class="n">enc_key</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">enc_iv</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_endpoint</span> <span class="o">=</span> <span class="n">endpoint</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_encoding</span> <span class="o">=</span> <span class="s2">&quot;utf-8&quot;</span>
        <span class="k">if</span> <span class="n">enc_key</span> <span class="o">==</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">enc_iv</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_transport_enc</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_transport_enc_key</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_transport_enc_iv</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cipher</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_transport_enc</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_transport_enc_key</span> <span class="o">=</span> <span class="n">enc_key</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_transport_enc_iv</span> <span class="o">=</span> <span class="n">enc_iv</span>
            <span class="n">backend</span> <span class="o">=</span> <span class="n">default_backend</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cipher</span> <span class="o">=</span> <span class="n">Cipher</span><span class="p">(</span><span class="n">algorithms</span><span class="o">.</span><span class="n">AES</span><span class="p">(</span>
                <span class="n">enc_key</span><span class="p">),</span> <span class="n">modes</span><span class="o">.</span><span class="n">CBC</span><span class="p">(</span><span class="n">enc_iv</span><span class="p">),</span> <span class="n">backend</span><span class="o">=</span><span class="n">backend</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_session</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>

<div class="viewcode-block" id="TDXBroker.call"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.call">[docs]</a>    <span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="n">json_obj</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;func&quot;</span><span class="p">:</span> <span class="n">func</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="n">params</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">json_obj</span><span class="p">[</span><span class="s2">&quot;params&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_transport_enc</span><span class="p">:</span>
            <span class="n">data_to_send</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encrypt</span><span class="p">(</span><span class="n">json_obj</span><span class="p">)</span>
            <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_session</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_endpoint</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">data_to_send</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_session</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_endpoint</span><span class="p">,</span> <span class="n">json</span><span class="o">=</span><span class="n">json_obj</span><span class="p">)</span>
        <span class="n">response</span><span class="o">.</span><span class="n">encoding</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_encoding</span>
        <span class="n">text</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">text</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_transport_enc</span><span class="p">:</span>
            <span class="n">decoded_text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">decrypt</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">decoded_text</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">decoded_text</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">text</span><span class="p">)</span></div>

<div class="viewcode-block" id="TDXBroker.encrypt"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.encrypt">[docs]</a>    <span class="k">def</span> <span class="nf">encrypt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source_obj</span><span class="p">):</span>
        <span class="n">encrypter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cipher</span><span class="o">.</span><span class="n">encryptor</span><span class="p">()</span>
        <span class="n">source</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">source_obj</span><span class="p">)</span>
        <span class="n">source</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_encoding</span><span class="p">)</span>
        <span class="n">need_to_padding</span> <span class="o">=</span> <span class="mi">16</span> <span class="o">-</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">source</span><span class="p">)</span> <span class="o">%</span> <span class="mi">16</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">need_to_padding</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">source</span> <span class="o">=</span> <span class="n">source</span> <span class="o">+</span> <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\x00</span><span class="s1">&#39;</span> <span class="o">*</span> <span class="n">need_to_padding</span>
        <span class="n">enc_data</span> <span class="o">=</span> <span class="n">encrypter</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">source</span><span class="p">)</span> <span class="o">+</span> <span class="n">encrypter</span><span class="o">.</span><span class="n">finalize</span><span class="p">()</span>
        <span class="n">b64_enc_data</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">encodebytes</span><span class="p">(</span><span class="n">enc_data</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">quote</span><span class="p">(</span><span class="n">b64_enc_data</span><span class="p">)</span></div>

<div class="viewcode-block" id="TDXBroker.decrypt"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.decrypt">[docs]</a>    <span class="k">def</span> <span class="nf">decrypt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source</span><span class="p">):</span>
        <span class="n">decrypter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cipher</span><span class="o">.</span><span class="n">decryptor</span><span class="p">()</span>
        <span class="n">source</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">unquote</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>
        <span class="n">source</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">decodebytes</span><span class="p">(</span><span class="n">source</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">))</span>
        <span class="n">data_bytes</span> <span class="o">=</span> <span class="n">decrypter</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">source</span><span class="p">)</span> <span class="o">+</span> <span class="n">decrypter</span><span class="o">.</span><span class="n">finalize</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">data_bytes</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;</span><span class="se">\x00</span><span class="s2">&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_encoding</span><span class="p">)</span></div>

<div class="viewcode-block" id="TDXBroker.data_to_df"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.data_to_df">[docs]</a>    <span class="k">def</span> <span class="nf">data_to_df</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span>
        <span class="k">if</span> <span class="s1">&#39;data&#39;</span> <span class="ow">in</span> <span class="n">result</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;data&#39;</span><span class="p">]</span>
            <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">)</span></div>

    <span class="c1">#------ functions</span>

<div class="viewcode-block" id="TDXBroker.ping"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.ping">[docs]</a>    <span class="k">def</span> <span class="nf">ping</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;ping&quot;</span><span class="p">,</span> <span class="p">{})</span></div>

<div class="viewcode-block" id="TDXBroker.logon"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.logon">[docs]</a>    <span class="k">def</span> <span class="nf">logon</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">version</span><span class="p">,</span> <span class="n">yyb_id</span><span class="p">,</span> <span class="n">account_id</span><span class="p">,</span> <span class="n">trade_account</span><span class="p">,</span> <span class="n">jy_passwrod</span><span class="p">,</span> <span class="n">tx_password</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;logon&quot;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s2">&quot;ip&quot;</span><span class="p">:</span> <span class="n">ip</span><span class="p">,</span>
            <span class="s2">&quot;port&quot;</span><span class="p">:</span> <span class="n">port</span><span class="p">,</span>
            <span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="n">version</span><span class="p">,</span>
            <span class="s2">&quot;yyb_id&quot;</span><span class="p">:</span> <span class="n">yyb_id</span><span class="p">,</span>
            <span class="s2">&quot;account_no&quot;</span><span class="p">:</span> <span class="n">account_id</span><span class="p">,</span>
            <span class="s2">&quot;trade_account&quot;</span><span class="p">:</span> <span class="n">trade_account</span><span class="p">,</span>
            <span class="s2">&quot;jy_password&quot;</span><span class="p">:</span> <span class="n">jy_passwrod</span><span class="p">,</span>
            <span class="s2">&quot;tx_password&quot;</span><span class="p">:</span> <span class="n">tx_password</span>
        <span class="p">})</span></div>

<div class="viewcode-block" id="TDXBroker.logoff"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.logoff">[docs]</a>    <span class="k">def</span> <span class="nf">logoff</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">client_id</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;logoff&quot;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s2">&quot;client_id&quot;</span><span class="p">:</span> <span class="n">client_id</span>
        <span class="p">})</span></div>

<div class="viewcode-block" id="TDXBroker.query_data"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.query_data">[docs]</a>    <span class="k">def</span> <span class="nf">query_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">client_id</span><span class="p">,</span> <span class="n">category</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;query_data&quot;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s2">&quot;client_id&quot;</span><span class="p">:</span> <span class="n">client_id</span><span class="p">,</span>
            <span class="s2">&quot;category&quot;</span><span class="p">:</span> <span class="n">category</span>
        <span class="p">})</span></div>

<div class="viewcode-block" id="TDXBroker.send_order"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.send_order">[docs]</a>    <span class="k">def</span> <span class="nf">send_order</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">client_id</span><span class="p">,</span> <span class="n">category</span><span class="p">,</span> <span class="n">price_type</span><span class="p">,</span> <span class="n">gddm</span><span class="p">,</span> <span class="n">zqdm</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">quantity</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;send_order&quot;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;client_id&#39;</span><span class="p">:</span> <span class="n">client_id</span><span class="p">,</span>
            <span class="s1">&#39;category&#39;</span><span class="p">:</span> <span class="n">category</span><span class="p">,</span>
            <span class="s1">&#39;price_type&#39;</span><span class="p">:</span> <span class="n">price_type</span><span class="p">,</span>
            <span class="s1">&#39;gddm&#39;</span><span class="p">:</span> <span class="n">gddm</span><span class="p">,</span>
            <span class="s1">&#39;zqdm&#39;</span><span class="p">:</span> <span class="n">zqdm</span><span class="p">,</span>
            <span class="s1">&#39;price&#39;</span><span class="p">:</span> <span class="n">price</span><span class="p">,</span>
            <span class="s1">&#39;quantity&#39;</span><span class="p">:</span> <span class="n">quantity</span>
        <span class="p">})</span></div>

<div class="viewcode-block" id="TDXBroker.cancel_order"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.cancel_order">[docs]</a>    <span class="k">def</span> <span class="nf">cancel_order</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">client_id</span><span class="p">,</span> <span class="n">exchange_id</span><span class="p">,</span> <span class="n">hth</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;cancel_order&quot;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;client_id&#39;</span><span class="p">:</span> <span class="n">client_id</span><span class="p">,</span>
            <span class="s1">&#39;exchange_id&#39;</span><span class="p">:</span> <span class="n">exchange_id</span><span class="p">,</span>
            <span class="s1">&#39;hth&#39;</span><span class="p">:</span> <span class="n">hth</span>
        <span class="p">})</span></div>

<div class="viewcode-block" id="TDXBroker.get_quote"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.get_quote">[docs]</a>    <span class="k">def</span> <span class="nf">get_quote</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">client_id</span><span class="p">,</span> <span class="n">code</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;get_quote&quot;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;client_id&#39;</span><span class="p">:</span> <span class="n">client_id</span><span class="p">,</span>
            <span class="s1">&#39;code&#39;</span><span class="p">:</span> <span class="n">code</span><span class="p">,</span>
        <span class="p">})</span></div>

<div class="viewcode-block" id="TDXBroker.repay"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.repay">[docs]</a>    <span class="k">def</span> <span class="nf">repay</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">client_id</span><span class="p">,</span> <span class="n">amount</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s2">&quot;repay&quot;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;client_id&#39;</span><span class="p">:</span> <span class="n">client_id</span><span class="p">,</span>
            <span class="s1">&#39;amount&#39;</span><span class="p">:</span> <span class="n">amount</span>
        <span class="p">})</span></div>

<div class="viewcode-block" id="TDXBroker.receive_order"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.receive_order">[docs]</a>    <span class="k">def</span> <span class="nf">receive_order</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        0 限价委托； 上海限价委托 / 深圳限价委托</span>
<span class="sd">        1 市价委托(深圳对方最优价格)</span>
<span class="sd">        2 市价委托(深圳本方最优价格)    </span>
<span class="sd">        3 市价委托(深圳即时成交剩余撤销)</span>
<span class="sd">        4 市价委托(上海五档即成剩撤 / 深圳五档即成剩撤)</span>
<span class="sd">        5 市价委托(深圳全额成交或撤销)</span>
<span class="sd">        6 市价委托(上海五档即成转限价)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">send_order</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">client_id</span><span class="p">,</span> <span class="n">event</span><span class="o">.</span><span class="n">category</span><span class="p">,</span> <span class="n">event</span><span class="o">.</span><span class="n">price_type</span><span class="p">,</span><span class="n">event</span><span class="o">.</span><span class="n">gddm</span><span class="p">,</span><span class="n">event</span><span class="o">.</span><span class="n">zqdm</span><span class="p">,</span><span class="n">event</span><span class="o">.</span><span class="n">price</span><span class="p">,</span><span class="n">event</span><span class="o">.</span><span class="n">quantity</span><span class="p">)</span></div>
        <span class="c1">#client_id, category, price_type, gddm, zqdm, price, quantity</span>

<div class="viewcode-block" id="TDXBroker.run"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAMarket.html#QUANTAXIS.QAMarket.tdxRealBroker.TDXBroker.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">event</span><span class="p">):</span>
        <span class="k">pass</span></div></div>

    
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">os</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TDXBroker</span><span class="p">(</span><span class="n">endpoint</span><span class="o">=</span><span class="s2">&quot;http://10.11.5.175:10092/api&quot;</span><span class="p">,</span>
                      <span class="n">enc_key</span><span class="o">=</span><span class="sa">b</span><span class="s2">&quot;4f1cf3fec4c84c84&quot;</span><span class="p">,</span> <span class="n">enc_iv</span><span class="o">=</span><span class="sa">b</span><span class="s2">&quot;0c78abc083b011e7&quot;</span><span class="p">)</span>
    <span class="c1">#api = TdxTradeApi(endpoint=&quot;http://10.11.5.175:10092/api&quot;)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;---Ping---&quot;</span><span class="p">)</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">ping</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>

    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;---登入---&quot;</span><span class="p">)</span>
    <span class="n">acc</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s2">&quot;TDX_ACCOUNT&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
    <span class="n">password</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s2">&quot;TDX_PASS&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">logon</span><span class="p">(</span><span class="s2">&quot;202.108.253.186&quot;</span><span class="p">,</span> <span class="mi">7708</span><span class="p">,</span>
                       <span class="s2">&quot;8.23&quot;</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span>
                       <span class="n">acc</span><span class="p">,</span> <span class="n">acc</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>

    <span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">result</span><span class="p">[</span><span class="s2">&quot;success&quot;</span><span class="p">]:</span>
        <span class="n">client_id</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="s2">&quot;data&quot;</span><span class="p">][</span><span class="s2">&quot;client_id&quot;</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">15</span><span class="p">):</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;---查询信息 cate=</span><span class="si">%d</span><span class="s2">--&quot;</span> <span class="o">%</span> <span class="n">i</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">data_to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">query_data</span><span class="p">(</span><span class="n">client_id</span><span class="p">,</span> <span class="n">i</span><span class="p">)))</span>

        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;---查询报价---&quot;</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">data_to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_quote</span><span class="p">(</span><span class="n">client_id</span><span class="p">,</span> <span class="s1">&#39;600315&#39;</span><span class="p">)))</span>

        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;---登出---&quot;</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">logoff</span><span class="p">(</span><span class="n">client_id</span><span class="p">))</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, yutiansut.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.0.39',
            LANGUAGE:'python',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>